home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / Tutorials / Tut01_CreateDevice / CreateDevice.cpp next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  5.8 KB  |  181 lines

  1. //-----------------------------------------------------------------------------
  2. // File: CreateDevice.cpp
  3. //
  4. // Desc: This is the first tutorial for using Direct3D. In this tutorial, all
  5. //       we are doing is creating a Direct3D device and using it to clear the
  6. //       window.
  7. //
  8. // Copyright (c) 2000-2001 Microsoft Corporation. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #include <d3d8.h>
  11.  
  12.  
  13.  
  14.  
  15. //-----------------------------------------------------------------------------
  16. // Global variables
  17. //-----------------------------------------------------------------------------
  18. LPDIRECT3D8             g_pD3D       = NULL; // Used to create the D3DDevice
  19. LPDIRECT3DDEVICE8       g_pd3dDevice = NULL; // Our rendering device
  20.  
  21.  
  22.  
  23.  
  24. //-----------------------------------------------------------------------------
  25. // Name: InitD3D()
  26. // Desc: Initializes Direct3D
  27. //-----------------------------------------------------------------------------
  28. HRESULT InitD3D( HWND hWnd )
  29. {
  30.     // Create the D3D object, which is needed to create the D3DDevice.
  31.     if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
  32.         return E_FAIL;
  33.  
  34.     // Get the current desktop display mode
  35.     D3DDISPLAYMODE d3ddm;
  36.     if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
  37.         return E_FAIL;
  38.  
  39.     // Set up the structure used to create the D3DDevice. Most parameters are
  40.     // zeroed out. We set Windowed to TRUE, since we want to do D3D in a
  41.     // window, and then set the SwapEffect to "discard", which is the most
  42.     // efficient method of presenting the back buffer to the display.  And 
  43.     // we request a back buffer format that matches the current desktop display 
  44.     // format.
  45.     D3DPRESENT_PARAMETERS d3dpp; 
  46.     ZeroMemory( &d3dpp, sizeof(d3dpp) );
  47.     d3dpp.Windowed = TRUE;
  48.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  49.     d3dpp.BackBufferFormat = d3ddm.Format;
  50.  
  51.     // Create the Direct3D device. Here we are using the default adapter (most
  52.     // systems only have one, unless they have multiple graphics hardware cards
  53.     // installed) and requesting the HAL (which is saying we want the hardware
  54.     // device rather than a software one). Software vertex processing is 
  55.     // specified since we know it will work on all cards. On cards that support 
  56.     // hardware vertex processing, though, we would see a big performance gain 
  57.     // by specifying hardware vertex processing.
  58.     if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  59.                                       D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  60.                                       &d3dpp, &g_pd3dDevice ) ) )
  61.     {
  62.         return E_FAIL;
  63.     }
  64.  
  65.     // Device state would normally be set here
  66.  
  67.     return S_OK;
  68. }
  69.  
  70.  
  71.  
  72.  
  73. //-----------------------------------------------------------------------------
  74. // Name: Cleanup()
  75. // Desc: Releases all previously initialized objects
  76. //-----------------------------------------------------------------------------
  77. VOID Cleanup()
  78. {
  79.     if( g_pd3dDevice != NULL) 
  80.         g_pd3dDevice->Release();
  81.  
  82.     if( g_pD3D != NULL)
  83.         g_pD3D->Release();
  84. }
  85.  
  86.  
  87.  
  88.  
  89. //-----------------------------------------------------------------------------
  90. // Name: Render()
  91. // Desc: Draws the scene
  92. //-----------------------------------------------------------------------------
  93. VOID Render()
  94. {
  95.     if( NULL == g_pd3dDevice )
  96.         return;
  97.  
  98.     // Clear the backbuffer to a blue color
  99.     g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
  100.     
  101.     // Begin the scene
  102.     g_pd3dDevice->BeginScene();
  103.     
  104.     // Rendering of scene objects can happen here
  105.     
  106.     // End the scene
  107.     g_pd3dDevice->EndScene();
  108.     
  109.     // Present the backbuffer contents to the display
  110.     g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
  111. }
  112.  
  113.  
  114.  
  115.  
  116. //-----------------------------------------------------------------------------
  117. // Name: MsgProc()
  118. // Desc: The window's message handler
  119. //-----------------------------------------------------------------------------
  120. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  121. {
  122.     switch( msg )
  123.     {
  124.         case WM_DESTROY:
  125.             PostQuitMessage( 0 );
  126.             return 0;
  127.  
  128.         case WM_PAINT:
  129.             Render();
  130.             ValidateRect( hWnd, NULL );
  131.             return 0;
  132.     }
  133.  
  134.     return DefWindowProc( hWnd, msg, wParam, lParam );
  135. }
  136.  
  137.  
  138.  
  139.  
  140. //-----------------------------------------------------------------------------
  141. // Name: WinMain()
  142. // Desc: The application's entry point
  143. //-----------------------------------------------------------------------------
  144. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  145. {
  146.     // Register the window class
  147.     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
  148.                       GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
  149.                       "D3D Tutorial", NULL };
  150.     RegisterClassEx( &wc );
  151.  
  152.     // Create the application's window
  153.     HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice", 
  154.                               WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
  155.                               GetDesktopWindow(), NULL, wc.hInstance, NULL );
  156.  
  157.     // Initialize Direct3D
  158.     if( SUCCEEDED( InitD3D( hWnd ) ) )
  159.     { 
  160.         // Show the window
  161.         ShowWindow( hWnd, SW_SHOWDEFAULT );
  162.         UpdateWindow( hWnd );
  163.  
  164.         // Enter the message loop
  165.         MSG msg; 
  166.         while( GetMessage( &msg, NULL, 0, 0 ) )
  167.         {
  168.             TranslateMessage( &msg );
  169.             DispatchMessage( &msg );
  170.         }
  171.     }
  172.  
  173.     // Clean up everything and exit the app
  174.     Cleanup();
  175.     UnregisterClass( "D3D Tutorial", wc.hInstance );
  176.     return 0;
  177. }
  178.  
  179.  
  180.  
  181.